Refactor replacement alists
authorjustbur <justin@burkett.cc>
Mon, 6 Jul 2015 17:23:19 +0000 (13:23 -0400)
committerjustbur <justin@burkett.cc>
Mon, 6 Jul 2015 17:23:19 +0000 (13:23 -0400)
which-key.el

index 7c7e16753ba85da52b39be32d197a30bf1ec3954..7571f7242c36db2029d58a78c2cfa2b66ef47350 100644 (file)
   "Truncate the description of keys to this length.  Also adds
 \"..\".")
 (defvar which-key-key-replacement-alist
-  '((">". "") ("<" . "") ("left" ."←") ("right" . "→"))
-  "The strings in the car of each cons cell are replaced with the
+  '(("<\\(\\(C-\\|M-\\)*.+\\)>" . "\\1") ("\\(left\\)" ."←")
+    ("\\(right\\)" . "→"))
+    "The strings in the car of each cons cell are replaced with the
 strings in the cdr for each key.")
-(defvar which-key-general-replacement-alist
+(defvar which-key-description-replacement-alist
   '(("Prefix Command" . "prefix"))
   "See `which-key-key-replacement-alist'.  This is a list of cons
-cells for replacing any text, keys and descriptions.")
+cells for replacing descriptions.")
 (defvar which-key-buffer-name "*which-key*"
   "Name of which-key buffer.")
 (defvar which-key-popup-type 'minibuffer
@@ -303,14 +304,12 @@ of the intended popup."
         key-match desc-match unformatted formatted)
     (with-temp-buffer
       (describe-buffer-bindings buffer key)
-      (which-key/replace-strings-from-alist which-key-general-replacement-alist)
       (goto-char (point-max)) ; want to put last keys in first
       (while (re-search-backward
               (format "^%s \\([^ \t]+\\)[ \t]+\\(\\(?:[^ \t\n]+ ?\\)+\\)$"
                       key-str-qt)
               nil t)
-        (setq key-match (s-replace-all
-                         which-key-key-replacement-alist (match-string 1))
+        (setq key-match (match-string 1)
               desc-match (match-string 2)
               max-len-key (max max-len-key (length key-match))
               max-len-desc (max max-len-desc (length desc-match)))
@@ -364,15 +363,24 @@ of the intended popup."
           (goto-char (point-min)))))
     (cons act-height act-width)))
 
-(defun which-key/replace-strings-from-alist (replacements)
+(defun which-key/perform-replacements (key-desc-cons-list key-reps desc-reps &optional literal)
   "Find and replace text in buffer according to REPLACEMENTS,
 which is an alist where the car of each element is the text to
 replace and the cdr is the replacement text."
-  (dolist (rep replacements)
-    (save-excursion
-      (goto-char (point-min))
-      (while (or (search-forward (car rep) nil t))
-        (replace-match (cdr rep) t t)))))
+  (mapcar
+   (lambda (el)
+     (let ((key (car el)) (desc (cdr el)))
+       (dolist (key-rep key-reps)
+         (setq key
+               (if (string-match (car key-rep) key)
+                   (replace-match (cdr key-rep) t literal key)
+                 key)))
+       (dolist (desc-rep desc-reps)
+         (setq desc
+               (if (string-match (car desc-rep) desc)
+                   (replace-match (cdr desc-rep) t literal desc)
+                 desc)))
+       (cons key desc))) key-desc-cons-list))
 
 (defsubst which-key/truncate-description (desc)
   "Truncate DESC description to `which-key-max-description-length'."
@@ -385,6 +393,9 @@ replace and the cdr is the replacement text."
 strings (including text properties), and pad with spaces so that
 all are a uniform length.  MAX-LEN-KEY and MAX-LEN-DESC are the
 longest key and description in the buffer, respectively."
+  (setq unformatted (which-key/perform-replacements
+                     unformatted which-key-key-replacement-alist
+                     which-key-description-replacement-alist nil))
   (mapcar
    (lambda (key-desc-cons)
      (let* ((key (car key-desc-cons))